package com.example.backend.controller;

import com.example.backend.model.CartItem;
import com.example.backend.service.CartItemService;
import com.example.backend.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartItemController {
    @Autowired
    CartItemService service;

    @GetMapping
    public Response<List<CartItem>> findByUser(@RequestParam("id") Integer userid) {
        return Response.Ok(
                "cart of this user",
                service.findByUser(userid)
        );
    }

    @PostMapping
    public Response<Integer> insertOne(@RequestBody CartItem item) {
        int result = service.insertOne(item);
        return result != -1 ?
                Response.Ok("insert success", result)
                : Response.Error("insert failed", result);
    }

    @PutMapping
    public Response<Integer> updateOne(@RequestBody CartItem item) {
        int result = service.updateOne(item);
        return result != -1 ?
                Response.Ok("update success", result)
                : Response.Error("update failed", result);

    }

    @DeleteMapping
    public Response<Integer> deleteOne(@RequestParam("id") Integer id) {
        int result = service.deleteOne(id);
        return result != -1 ?
                Response.Ok("delete success", result)
                : Response.Error("delete failed", result);

    }
}
